$(function () {
    $("body").on('click','.order-status-box',function () {
        var _this = $(this);
        var id = _this.data("id");
        var status = _this.data("status");
        var message = '';
        if(status=='CANCEL'){
            message='确认要取消订单?';
            icModale(2,message,3,function () {
                updateOrderStatus(_this,status,id);
            });
        }else if(status=='SUCCESS'){
            message='确认要审核通过订单吗?';
            icModale(2,message,3,function () {
                orderSuccess(_this,id);
            });
        }else if(status=='REFUND'){
            message='<div>&nbsp;&nbsp;请确认用户是否学习过此课程，退费将会把购课金额退回至用户前台个人账户中。<p>&nbsp;&nbsp;确认要退费吗？</p></div>';
            icModale(2,message,3,function () {
                updateOrderStatus(_this,status,id);
            });
        }else if(status=='SENDOUT'){
            message='确认订单中的图书商品发货吗?';
            icModale(2,message,3,function () {
                //updateOrderStatus(_this,status,id);
                sendOutOrder(_this,id)
            });
        }
    });

    /**
     * 订单物流发货
     * @param em
     * @param id
     */
    function sendOutOrder(em,id) {
        parent.layer.open({
            type: 2,
            skin: 'layui-layer-demo',
            area: ['720px','520px'],
            title: '订单发货',
            content: baseloaction + '/admin/order/sendOutOrder',
            btn: '确定',
            yes: function(index, layero) {
                var iframeName = layero.find("iframe").attr('name');
                var mailName = $(parent.window.frames[iframeName].document).find("#mailName").val();
                var mailCode = $(parent.window.frames[iframeName].document).find("#mailCode").val();
                var mailTime = $(parent.window.frames[iframeName].document).find("#mailTime").val();
                var s = updateOrderStatus(em,'SENDOUT',id,'','','',mailName,mailCode,mailTime);
                if(s==true){
                    parent.layer.close(index);
                    icModale(0,'发货成功!');
                }
            }

        });
    }

    /**
     * 审核订单窗口
     * @param em
     * @param id
     */
    function orderSuccess(em,id) {
        parent.layer.open({
            type: 2,
            skin: 'layui-layer-demo',
            area: ['720px','520px'],
            title: '审核订单',
            content: baseloaction + '/admin/order/auditionOrderPage',
            btn: '确定',
            yes: function(index, layero) {
                var iframeName = layero.find("iframe").attr('name');
                var payType = $(parent.window.frames[iframeName].document).find("#payType").val();
                var payTime = $(parent.window.frames[iframeName].document).find("#payTime").val();
                var externalOrderNo = $(parent.window.frames[iframeName].document).find("#externalOrderNo").val();
                icModale(0,'订单数据校验中，请稍等一会，请不要重复操作！！');
                var s = updateOrderStatus(em,'SUCCESS',id,payTime,payType,externalOrderNo);
                if(s==true){
                    icModale(0,'订单审核成功！');
                    parent.layer.close(index);
                }
            }
        });
    }

    /**
     * 修改订单状态
     * @param em 操作的元素
     * @param status 状态
     * @param orderId  订单ID
     * @param payTime 支付时间
     * @param payType 支付类型
     * @param mailName 物流公司名
     * @param mailCode 物流单号
     * @param mailTime 发货时间
     */
    function updateOrderStatus(em,status,orderId,payTime,payType,externalOrderNo,mailName,mailCode,mailTime) {
        var s = true;
        if(typeof payTime =='undefined' || payTime==null){
            payTime='';
        }
        if(typeof payType =='undefined' || payType==null){
            payType = '';
        }
        if(typeof externalOrderNo =='undefined' || externalOrderNo==null){
            externalOrderNo = '';
        }
        if(typeof mailName =='undefined' || mailName==null){
            mailName = '';
        }
        if(typeof mailCode =='undefined' || mailCode==null){
            mailCode = '';
        }
        if(typeof mailTime =='undefined' || mailTime==null){
            mailTime = '';
        }
        $.ajax({
            url:baseloaction +'/admin/order/updateOrderStatus',
            type:'post',
            dataType:'json',
            async:false,
            data:{
                'id':orderId,
                'orderStatus':status,
                'payTime':payTime,
                'payType':payType,
                'externalOrderNo':externalOrderNo,
                'mailName':mailName,
                'mailCode':mailCode,
                'mailTime':mailTime
            },
            success:function (result) {
                s = result.success;
                if(result.success==true){
                    var obj = result.entity;
                    if(status =='SUCCESS'){//审核成功
                        $("#order-pay-time-"+orderId).text(obj.payTime.substring(0,16));
                        $("#order-status-box-"+orderId).html('<span class="label label-info">已支付</span>');
                        em.parent().find(".order-status-box").remove();
                        var payTypeText = '';
                        if(obj.payType=='ALIPAY'){
                            payTypeText='支付宝';
                        }else if(obj.payType=='WEIXIN'){
                            payTypeText='微信';
                        }else if(obj.payType=='IOS'){
                            payTypeText = 'IOS内购';
                        }else if(obj.payType=='ACCOUNT'){
                            payTypeText = '账户支付';
                        }
                        $("#order-pay-type-box-"+orderId).text(payTypeText);
                        var sellTypeList = obj.sellTypeList;
                        if(sellTypeList.indexOf(',QA,')==-1
                            && sellTypeList.indexOf(',ACCOUNT,')==-1
                            && sellTypeList.indexOf(',EXAM,')==-1
                            && sellTypeList.indexOf(',ATTEND,')==-1 && obj.payType!='IOS'  && sellTypeList.indexOf(',MEMBER,')==-1){
                            $("#success-box-"+orderId).append('<a  href="javascript:void(0)" class="c-master fs14 order-status-box" data-id="'+orderId+'" data-status="REFUND" data-toggle="tooltip" data-placement="bottom" data-original-title="退费"><i class="fa fa-sign-out"></i></a>')
                        }
                        if(sellTypeList.indexOf(',BOOK,')!=-1){
                            $("#success-box-"+orderId).append('<a href="javascript:void(0)" class="c-master fs14 order-status-box" data-id="'+orderId+'" data-status="SENDOUT" data-toggle="tooltip" data-placement="bottom" data-original-title="发货"><i class="fa fa-send-o"></i></a>')
                        }
                    }
                    if(status == 'REFUND'){//退费
                        $("#order-status-box-"+orderId).html('<span class="label label-primary">已退费</span>');
                        em.parent().find('.order-status-box').remove();
                    }
                    if(status == 'CANCEL'){//取消
                        em.parent().find('.order-status-box').remove();
                        $("#order-status-box-"+orderId).html('<span class="label label-default">已取消</span>');
                    }
                    if(status =='SENDOUT'){//发货
                        em.remove();
                    }
                }else{
                    icModale(0,result.message);
                }

            },
            error:function (error) {
                s = false;
                icModale(0,'系统错误，操作失败');
            }
        });
        return s;
    }
});